﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AppServidor
{
    class ConectorAndroid
    {
            static Socket listeningSocket;
            static Socket socket;
            static Thread thrReadRequest;
            static int iPort = 9040;
            static int iConnectionQueue = 10;

            static void Main(string[] args)
            {
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, iPort);
                listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                              
                    
                try
                {
                    listeningSocket.Bind(localEndPoint);
                    listeningSocket.Listen(iConnectionQueue);

                    thrReadRequest = new Thread(new ThreadStart(getRequest));
                    thrReadRequest.Start();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Winsock error: " + e.ToString());
                }
            }

            static private void getRequest()
            {
                int i = 0;
                while (true)
                {
                    i++;
                    Console.WriteLine("Outside Try i = {0}", i.ToString());

                    try
                    {
                        socket = listeningSocket.Accept();
                        byte[] buffer = new byte[socket.SendBufferSize];
                        int iBufferLength = socket.Receive(buffer, 0, buffer.Length, 0);
                        Console.WriteLine("Received {0}", iBufferLength);
                        Array.Resize(ref buffer, iBufferLength);
                        string formattedBuffer = Encoding.ASCII.GetString(buffer);

                        Console.WriteLine("Data received by Client: {0}", formattedBuffer);

                        if (formattedBuffer == "quit")
                        {
                            socket.Close();
                            listeningSocket.Close();
                            Environment.Exit(0);
                        }

                      /*  Console.WriteLine("Inside Try i = {0}", i.ToString());
                        Thread.Sleep(500);*/
                        socket.Shutdown(SocketShutdown.Both);
                    }
                    catch (Exception e)
                    {
                        //socket.Close();
                        Console.WriteLine("Receiving error: " + e.ToString());
                        Console.ReadKey();
                        //throw;
                    }

                    finally
                    {
                        socket.Close();
                        //listeningsocket.close();
                    }
                }
            }
    }
}
